#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <thread>
#include <sw/redis++/redis++.h>

using namespace std;
using namespace sw::redis;
using namespace std::chrono_literals;

void test1(Redis &redis)
{
    cout << "using hset and hget" << endl;
    redis.hset("key", "f1", "111");
    redis.hset("key", make_pair("f2", "222"));
    redis.hset("key",
               {make_pair("f3", "333"),
                make_pair("f4", "444")});
    vector<pair<string, string>> values = {make_pair("f5", "555"), make_pair("f6", "666")};
    redis.hset("key", values.begin(), values.end());

    // auto ret = redis.hget("key", "f5");
    // if(ret) cout << "f5 = " << ret.value() << endl;
    cout << redis.hexists("key", "f4") << endl;
    cout << redis.hdel("key", {"f1", "f2"}) << endl;
    cout << redis.hlen("key") << endl;
}

void test2(Redis &redis)
{
    cout << "using hkeys and hvals" << endl;
    redis.hset("key", "f1", "111");
    redis.hset("key", make_pair("f2", "222"));
    redis.hset("key",
               {make_pair("f3", "333"),
                make_pair("f4", "444")});
    // vector<pair<string, string>> values = {make_pair("f5", "555"), make_pair("f6", "666")};
    // redis.hset("key", values.begin(), values.end());

    vector<string> rets;
    auto it = back_inserter(rets);
    redis.hkeys("key", it);
    for (auto &ret : rets)
        cout << ret << endl;

    cout << endl;

    vector<string> values;
    auto it2 = back_inserter(values);
    redis.hvals("key", it2);
    for (auto &ret : values)
        cout << ret << endl;
}

void test3(Redis& redis)
{
    cout << "using hmset and hmget" << endl;
    redis.hmset("key",
    {
        make_pair("f1", "111"),
        make_pair("f2", "222"),
        make_pair("f3", "333"),
    });

    vector<string> values;
    auto it = back_inserter(values);
    redis.hmget("key", {"f1", "f2", "f3"}, it);
    for(auto& ret : values)
        cout << ret << endl;
}

int main()
{
    sw::redis::Redis redis("tcp://127.0.0.1:6379");
    // test1(redis);
    // test2(redis);
    test3(redis);
    redis.flushall();
    return 0;
}